﻿using UnityEngine;
using UnityEditor;
using UnityEditor.UI;
using UnityEngine.UI;
using Debug = System.Diagnostics.Debug;

namespace SoarD.Editor.UI
{
    [CustomEditor(typeof(SDEffectSprite), true)]
    [CanEditMultipleObjects]
    public class SDEffectSpriteEditor : GraphicEditor
    {
        SerializedProperty m_Sprite;
        SerializedProperty m_TargetPosition;
        SerializedProperty m_PosTimeTotal;
        SerializedProperty m_TargetRotate;
        SerializedProperty m_RotateTimeTotal;
        SerializedProperty m_TargetColor;
        SerializedProperty m_ColorTimeTotal;
        SerializedProperty m_TargetScale;
        SerializedProperty m_ScaleTimeTotal;
        SerializedProperty m_LoopMode;

        GUIContent m_SpriteContent;
        GUIContent m_TargetPositionContent;
        GUIContent m_PosTimeTotalContent;
        GUIContent m_TargetRotateContent;
        GUIContent m_RotateTimeContent;
        GUIContent m_TargetColorContent;
        GUIContent m_ColorTimeTotalContent;
        GUIContent m_TargetScaleContent;
        GUIContent m_ScaleTimeTotalContent;
        GUIContent m_LoopModeContent;
        protected override void OnEnable()
        {
            base.OnEnable();
            m_SpriteContent = new GUIContent("Source Image");
            m_TargetPositionContent = new GUIContent("目标:");
            m_PosTimeTotalContent = new GUIContent("时间:");
            m_TargetRotateContent = new GUIContent("目标:");
            m_RotateTimeContent = new GUIContent("时间:");
            m_TargetColorContent = new GUIContent("目标:");
            m_ColorTimeTotalContent = new GUIContent("时间:");
            m_TargetScaleContent = new GUIContent("目标:");
            m_ScaleTimeTotalContent = new GUIContent("时间:");
            m_LoopModeContent = new GUIContent("循环模式");

            m_Sprite = serializedObject.FindProperty("m_Sprite");
            m_TargetPosition = serializedObject.FindProperty("m_TargetPosition");
            m_PosTimeTotal = serializedObject.FindProperty("m_PosTimeTotal");
            m_TargetRotate = serializedObject.FindProperty("m_TargetRotate");
            m_RotateTimeTotal = serializedObject.FindProperty("m_RotateTimeTotal");
            m_TargetColor = serializedObject.FindProperty("m_TargetColor");
            m_ColorTimeTotal = serializedObject.FindProperty("m_ColorTimeTotal");
            m_TargetScale = serializedObject.FindProperty("m_TargetScale");
            m_ScaleTimeTotal = serializedObject.FindProperty("m_ScaleTimeTotal");
            m_LoopMode = serializedObject.FindProperty("m_LoopMode");
        }


        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            SpriteGUI();
            AppearanceControlsGUI();
            EffectGUI();
            serializedObject.ApplyModifiedProperties();
        }

        protected void SpriteGUI()
        {
            EditorGUILayout.PropertyField(m_Sprite, m_SpriteContent);
        }


        protected void EffectGUI()
        {
            SDEffectSprite _SDSprite = target as SDEffectSprite;
            Debug.Assert(_SDSprite != null, "_SDSprite != null");
            InspectorHelper.BeginGroup();
            _SDSprite.positionEnable = InspectorHelper.Toggle("位置", _SDSprite.positionEnable, true);
            if (_SDSprite.positionEnable)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(m_TargetPosition, m_TargetPositionContent);
                EditorGUILayout.PropertyField(m_PosTimeTotal, m_PosTimeTotalContent);
                EditorGUI.indentLevel--;
            }
            InspectorHelper.EndGroup();
            InspectorHelper.BeginGroup();
            _SDSprite.rotateEnable = InspectorHelper.Toggle("旋转", _SDSprite.rotateEnable, true);
            if (_SDSprite.rotateEnable)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(m_TargetRotate, m_TargetRotateContent);
                EditorGUILayout.PropertyField(m_RotateTimeTotal, m_RotateTimeContent);
                EditorGUI.indentLevel--;
            }
            InspectorHelper.EndGroup();
            InspectorHelper.BeginGroup();
            _SDSprite.colorEnable = InspectorHelper.Toggle("色彩", _SDSprite.colorEnable, true);
            if (_SDSprite.colorEnable)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(m_TargetColor, m_TargetColorContent);
                EditorGUILayout.PropertyField(m_ColorTimeTotal, m_ColorTimeTotalContent);
                EditorGUI.indentLevel--;
            }
            InspectorHelper.EndGroup();
            InspectorHelper.BeginGroup();
            _SDSprite.scaleEnable = InspectorHelper.Toggle("缩放", _SDSprite.scaleEnable, true);
            if (_SDSprite.scaleEnable)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(m_TargetScale, m_TargetScaleContent);
                EditorGUILayout.PropertyField(m_ScaleTimeTotal, m_ScaleTimeTotalContent);
                EditorGUI.indentLevel--;
            }
            InspectorHelper.EndGroup();
            InspectorHelper.BeginGroup();
            _SDSprite.useLoop = InspectorHelper.Toggle("UseLoop", _SDSprite.useLoop, true);
            if (_SDSprite.useLoop)
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(m_LoopMode, m_LoopModeContent);
                EditorGUI.indentLevel--;
            }
            InspectorHelper.EndGroup();
        }

        public override bool HasPreviewGUI() { return true; }


        public override string GetInfoString()
        {
            Image image = target as Image;
            Sprite sprite = image.sprite;
            int x = (sprite != null) ? Mathf.RoundToInt(sprite.rect.width) : 0;
            int y = (sprite != null) ? Mathf.RoundToInt(sprite.rect.height) : 0;

            return string.Format("Image Size: {0}x{1}", x, y);
        }
    }
}
